﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class DisplayScore : System.Web.UI.Page
    {
        public static Module_Page Pages = new Module_Page();
        protected int examid = 0;
        private int areaid = 0, schoolid = 0, gradeid = 0, classid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "InputScore"))
            {
                Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                Response.Redirect("login.aspx");
                Response.End();
            }
            if (!PageValidate.QueryStringIsEmpty("examid"))
            {
                examid = int.Parse(PageValidate.QueryString("examid"));
            }
            else
            {
                Response.Redirect("AdminExamRoom.aspx");
                return;
            }
            if (Request.QueryString["page"] != null && Request.QueryString["page"] != string.Empty)
            {
                if (PageValidate.IsNumber(Request.QueryString["page"]))
                {
                    Pages.PageNum = int.Parse(Request.QueryString["page"]);
                }
            }
            if (!PageValidate.QueryStringIsEmpty("areaid"))
            { 
                areaid = int.Parse(PageValidate.QueryString("areaid"));
            }
            if (!PageValidate.QueryStringIsEmpty("schoolid"))
            {
                schoolid = int.Parse(PageValidate.QueryString("schoolid"));
            }
            if (!PageValidate.QueryStringIsEmpty("gradeid"))
            {
                gradeid = int.Parse(PageValidate.QueryString("gradeid"));
            }
            if (!PageValidate.QueryStringIsEmpty("classid"))
            {
                classid = int.Parse(PageValidate.QueryString("classid"));
            }
            if (!IsPostBack)
            {
                BindStudentScore();
                BindNavigation();
            }
        }

        protected string GetAreaName(string id)
        {
            return Area.GetSingleInfo(int.Parse(id)).Name;
        }
        protected string GetSchoolName(string id)
        {
            return School.GetSingleInfo(int.Parse(id)).Name;
        }
        protected string GetGradeName(string id)
        {
            return Grade.GetSingleInfo(int.Parse(id)).Name;
        }
        protected string GetClassName(string id)
        {
            return TClass.GetSingleInfo(int.Parse(id)).Name;
        }
        private void BindNavigation()
        {
            StringBuilder sb = new StringBuilder();
            string url = "DisplayScore.aspx?examid=" + examid;
            if (areaid > 0)
            {
                AreaInfo areaInfo = Area.GetSingleInfo(areaid);
                NavSelect.Text = " >>> <a href='" + url + "&areaid=" + areaInfo.Id + "'>" + areaInfo.Name + "</a>";
                if (schoolid > 0)
                {
                    SchoolInfo schoolInfo = School.GetSingleInfo(schoolid);
                    NavSelect.Text += " >>> <a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolInfo.Id + "'>" + schoolInfo.Name + "</a>";
                    if (gradeid > 0)
                    {
                        GradeInfo gradeInfo = Grade.GetSingleInfo(gradeid);
                        NavSelect.Text += " >>> <a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolid + "&gradeid=" + gradeInfo.Id + "'>" + gradeInfo.Name + "</a>";
                        if (classid > 0)
                        {
                            TClassInfo classInfo = TClass.GetSingleInfo(classid);
                            NavSelect.Text += " >>> <a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolid + "&gradeid=" + gradeid + "&classid=" + classid + "'>" + classInfo.Name + "</a>";
                        }
                        else
                        {
                            IList<TClassInfo> classInfos = new List<TClassInfo>();
                            classInfos = TClass.GetClassInfoByGradeId(gradeid);
                            foreach (TClassInfo classInfo in classInfos)
                            {
                                sb.Append("<a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolid + "&gradeid=" + gradeid + "&classid=" + classInfo.Id.ToString() + "'> ");
                                sb.Append(classInfo.Name);
                                sb.Append("</a>");
                            }
                        }
                    }
                    else
                    {
                        IList<GradeInfo> gradeInfos = new List<GradeInfo>();
                        gradeInfos = Grade.GetGradeInfoBySchoolId(schoolid);
                        foreach (GradeInfo gradeInfo in gradeInfos)
                        {
                            sb.Append("<a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolid + "&gradeid=" + gradeInfo.Id.ToString() + "'> ");
                            sb.Append(gradeInfo.Name);
                            sb.Append("</a>");
                        }
                    }
                }
                else
                {
                    IList<SchoolInfo> schoolInfos = new List<SchoolInfo>();
                    schoolInfos = School.GetSchoolInfoByAreaId(areaid);
                    foreach (SchoolInfo schoolInfo in schoolInfos)
                    {
                        sb.Append("<a href='" + url + "&areaid=" + areaid + "&schoolid=" + schoolInfo.Id.ToString() + "'> ");
                        sb.Append(schoolInfo.Name);
                        sb.Append("</a>");
                    }
                }
            }
            else
            {
                ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examid);
                if (examroomInfo.ExamAreaId != null)
                {
                    string[] arr_areaid = examroomInfo.ExamAreaId.Split(',');
                    string[] arr_areaname = examroomInfo.ExamAreaName.Split(',');
                    for (int i = 0; i < arr_areaid.Length; i++)
                    {
                        sb.Append("<a href='" + url + "&areaid=" + arr_areaid[i] + "'> ");
                        sb.Append(arr_areaname[i]);
                        sb.Append("</a>");
                    }
                }
            }
            NavLabel.Text = sb.ToString();
        }
        private void BindStudentScore()
        {
            Pages.AllCount = StudentScoreExtend.GetCount(examid, areaid, schoolid, gradeid, classid, 0);
            Pages.PageSize = 20;
            Pages.PageName = GetPageName();
            Pages.Compute();
            this.PageLabel.Text = Pages.GetPage();
            IList<StudentScoreExtendInfo> infos = new List<StudentScoreExtendInfo>();
            infos = StudentScoreExtend.GetList(examid, areaid, schoolid, gradeid, classid, 0, Pages.First, Pages.Max);
            StudentScoreList.DataSource = infos;
            StudentScoreList.DataBind();
        }

        private string GetPageName()
        {
            StringBuilder builder = new StringBuilder();
            
            builder.Append("DispLayScore.aspx?id=1");
            if (examid > 0)
            {
                builder.Append("&examid=");
                builder.Append(this.examid);
                if (areaid > 0)
                {
                    builder.Append("&areaid=");
                    builder.Append(this.areaid);
                    if (schoolid > 0)
                    {
                        builder.Append("&schoolid=");
                        builder.Append(this.schoolid);
                        if (gradeid > 0)
                        {
                            builder.Append("&gradeid=");
                            builder.Append(this.gradeid);
                            if (classid > 0)
                            {
                                builder.Append("&classid=");
                                builder.Append(this.classid);
                            }
                        }
                    }
                }
            }
            builder.Append("&Page=");
            
            return builder.ToString();
        }
    }
}
